package com.example.xutildemoproject.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.example.xutildemoproject.R;
import com.xuexiang.constant.PermissionConstants;
import com.xuexiang.xaop.annotation.Permission;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageFragment;
import com.xuexiang.xutil.app.IntentUtils;
import com.xuexiang.xutil.app.PathUtils;
import com.xuexiang.xutil.common.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

@Page(name = "Uri解析")
public class UriPathFragment extends XPageFragment {
    public static final int REQUEST_FILE = 1000;
    @BindView(R.id.et_content)
    EditText etContent;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_uri_path;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {

    }

    @OnClick(R.id.btnSelect)
    protected void onViewClicked(View view){
        selectFile();
    }

    @Permission(PermissionConstants.STORAGE)
    private void selectFile(){
        startActivityForResult(IntentUtils.getDocumentPickerIntent(IntentUtils.DocumentType.ANY),REQUEST_FILE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_FILE) {
                if (data != null) {
                    Uri uri = data.getData();
                    String filePath = PathUtils.getFilePathByUri(getContext(),uri);
                    if (StringUtils.isEmptyTrim(filePath)) {
                        filePath = "未解析成功";
                    }else{
                        filePath = "文件路径:" + filePath;
                    }
                    etContent.setText(filePath);
                }

            }
        }
    }
}
